from machine import I2C, Pin from time import sleep_ms # MCP4725 DAC constants MCP4725_ADDR = 0x60 DAC_REGISTER = 0x40 # Raspberry Pi Pico constants SDA_PIN = 8 #physical pin 11 SCL_PIN = 9 #physical pin 12 ########################################################################## def scan_i2c(): i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=100000) # Adjust pins and frequency as needed devices = i2c.scan() if devices: print("I2C devices found:") for device in devices: print(hex(device)) else: print("No I2C devices found.") scan_i2c() ######################################################################### # Initialize I2C i2c = I2C(0, sda=Pin(SDA_PIN), scl=Pin(SCL_PIN), freq=100000) # Function to set DAC output def set_dac_value(value): # Ensure value is within the valid range (0 to 4095) value = max(0, min(value, 4095)) # Construct the data array #data = [(DAC_REGISTER | ((value >> 8) & 0x0F)), (value & 0xFF)] data = bytearray([(value >> 8) & 0xFF, value & 0xFF]) #print("Setting DAC to value:", value) #print("DAC DATA:", data) # Write data to the DAC i2c.writeto(MCP4725_ADDR, data) command_byte = (DAC_REGISTER | ((value >> 8) & 0x0F)) print("Command Byte:", bin(command_byte)) try: while True: for value in range(0, 4096, 256): # Increase the step size if needed set_dac_value(value) sleep_ms(1000) # Adjust delay as needed except KeyboardInterrupt: # Clean up and exit on Ctrl+C del i2c